package com.yuqiao.read.lambda;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Lambda3 {
    public static void main(String[] args) {
        IntStream intStream = IntStream.range(1, 100).filter(i -> i % 2 == 0);
        System.out.println("args = [" + intStream.count() + "]");
        Stream<int[]> pythagoreanTriples = IntStream.rangeClosed(1, 100).boxed()
                .flatMap(a -> IntStream.rangeClosed(a, 100)
                        .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0)
                        .mapToObj(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)}));
        pythagoreanTriples.forEach(t->System.out.println(t[0] + ","+ t[1]+ ","+t[2]));
        IntStream.rangeClosed(0,100).boxed()
                .flatMap(a->IntStream.rangeClosed(a,100)
                .mapToObj(b->new double[]{a,b,Math.sqrt(a*a + b*b)})
                .filter(t->t[2] %1==0));

        long uniqueWords = 0;
        try {
            Stream<String> lines = Files.lines(Paths.get("E:\\selfproject\\book-commons\\src\\main\\java\\com\\yuqiao\\read\\TestUse.java"), Charset.defaultCharset());
            uniqueWords = lines.flatMap(line-> Arrays.stream(line.split(" ")))
                    .distinct()
                    .count();
            System.out.println("args = [" + uniqueWords + "]");
        } catch (IOException e) {
            e.printStackTrace();
        }


        Stream.iterate(new int[]{0,1},t->new int[]{t[1],t[0]+t[1]})
                .limit(100)
                .map(p->p[0])
                .forEach(System.out::println);
    }
}
